package jmx;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
  

  
public class ReadXMLByDom4j {  
    //创建一个存放metric的list 
    private List<Metric> MetricList = null;
    private Metric metric = null;  
    public  String mbean = null; 
    public List<Metric> getMetrics(File file){  
          
        SAXReader reader = new SAXReader();  
        try {  
        	//通过解析器读取文件
            Document document = reader.read(file); 
            //得到根元素<cassandraMbean>
            Element mbeanStore = document.getRootElement();  
            //返回根元素的迭代器，所有的子元素<mbean>
            Iterator mbeanit = mbeanStore.elementIterator();  
            MetricList = new ArrayList<Metric>();  
            while(mbeanit.hasNext()){ 
            	//遍历所有的子元素
               
                //获取mbean子元素
                Element MbeanElement = (Element) mbeanit.next();  
                 //遍历MetricElement的属性  
                 //获得mbean的属性
                List<Attribute> attributes = MbeanElement.attributes();  
                
                //遍历mbean元素的属性
                for(Attribute attribute : attributes){  
                	//如果属性名称为name，则获得属性值
                    if(attribute.getName()=="name"){  
                       mbean = attribute.getValue();//System.out.println(id);  
                    
                         
                    }  
                }  
                 //获得mbean的metric子元素的迭代器
                Iterator Metricit = MbeanElement.elementIterator();  
                //metric
                while(Metricit.hasNext()){ 
                	 metric = new Metric(); 
                	
                    Element metrics = (Element) Metricit.next(); 
                    Iterator info = metrics.elementIterator();
                    //metric中的node
                    
                    while(info.hasNext()){
                    	Element metricinfo = (Element)info.next();
                    	String nodeName = metricinfo.getName();  
                    
                        if(nodeName=="name"){  
                            //System.out.println(child.getStringValue());  
                            String name = metricinfo.getStringValue(); 
                            metric.setName(name);  
                        }else if(nodeName=="value"){  
                        	//獲取xml中的指標名
                            String value = metricinfo.getStringValue(); 
                            metric.setValue(value);  
                        }
                    }
                    metric.setMbean(mbean); 
                    //將metric的相關信息加入到list中
                    MetricList.add(metric);
                    metric = null;
                
                   // System.out.println(metric.getMbean());
                    
                }  
               //MetricList.add(metric);  
              
                 
                  
            }  
        } catch (DocumentException e) {  
          
            e.printStackTrace();  
        }  
          
          
        return MetricList;  
          
    }  
      
  
    /** 
     * @param args 
     */  
   
  
}